<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title></title>

    <link rel="stylesheet" th:href="@{/res/css/amazeui.min.css}">
    <link rel="stylesheet" th:href="@{/res/css/admin.css}">
    <link rel="stylesheet" th:href="@{/res/layui/css/layui.css}">

    <style>
        #confirm {
            margin-left: 30%;
        }

        #cancel {
            margin-left: 10%;
        }
    </style>
</head>

<body style="overflow: auto;">
<div class="admin-content-body">

    <div class="am-g" style="margin-top: 5px;">
        <div class="am-u-sm-12">

            <form class="am-form">
                <table class="am-table am-table-striped am-table-hover table-main">
                    <thead>
                    <tr>
                        <th class="table-id">
                            菜单名称
                        </th>

                        <th class="table-id">
                            权限状态
                        </th>
                    </tr>
                    </thead>
                    <tbody id="menuTable">

                    </tbody>
                </table>

            </form>
            <div id="btn">
                <button id="confirm" class="am-btn am-btn-primary">确认</button>
                <button id="cancel" class="am-btn am-btn-primary">取消</button>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/js/jquery-1.11.3.min.js}"></script>
<script th:src="@{/myplugs/js/plugs.js}"></script>
<script>
    //解析url
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    $(function () {
        var Request = new Object();
        Request = GetRequest();
        var length;
        var type = Request["type"];
        console.log(type);
        var $tbody = $("#menuTable");
        var dataList;
        $.ajax({
            type: "post",
            url: "/menu/selectMenu2",
            data: {type: type},
            dataType: 'json',
            success: function (data) {
                console.log(data);
                dataList = data;
                var newHtml = '';
                length = data.length;
                for (var i = 0; i < data.length; i++) {
                    newHtml += '<tr>';
                    newHtml += '<td> <a href="javascript:void(0)">' + data[i].name + '</a> </td>';
                    newHtml += '<td>';
                    newHtml += '<div class="am-btn-toolbar">';
                    newHtml += '<div class="am-btn-group am-btn-group-xs">';
                    newHtml += '<input  type="checkbox" id="chk_' + (i + 1) + '" checked >';
                    newHtml += '</input>';
                    newHtml += '</div>';
                    newHtml += '</div>';
                    newHtml += '</td>';
                    newHtml += ' </tr>';


                }
                $tbody.html(newHtml);
                for (var i = 0; i < data.length; i++) {
                    if (data[i].status == 0)
                        $("#chk_" + (i + 1)).removeAttr("checked");
                }
            },
            error: function () {
                alert(error);
            }
        })

        $("#confirm").click(function () {
            for (var i = 0; i < length; i++) {
                if ($("#chk_" + (i + 1)).is(':checked')) {
                    dataList[i].status = 1;
                } else {
                    dataList[i].status = 0;
                }
            }
            console.log(dataList[1]);
            for (var i = 0; i < length; i++) {
                $.ajax({
                    type: "post",
                    url: "/menu/updateMenuList",
                    dataType: "text",
                    data: {
                        id: dataList[i].id,
                        name: dataList[i].name,
                        menu_id: dataList[i].menuId,
                        level: dataList[i].level,
                        status: dataList[i].status
                    },
                    success: function (result) {
                        //alert(result);
                    },
                    error: function (result) {
                        config.log(result);
                    }
                })
            }
            alert("处理成功!");
            parent.location.reload();
        })


    });
</script>
</body>
</html>